var API = API || {};

API.Google = {
    MapsManager: function(map_obj, gh_obj, google_map_component, map_type) {
        var parent = this;

        this.map_obj = map_obj;
        this.gh_obj = gh_obj;
        this.google_map_component = google_map_component;
        this.map_type = map_type;

//    	this.googleMapsEventHandler = function(marker) {
//            parent.OpenInfoWidow(marker);
//        };

        this.gh_objHandler = function() {
            parent.OpenInfoWidow(parent.markers[$(this).attr('data-id')]); // Bind Links With Info Window
        };

        var icon = 'hotel.png';
        var startX = 0;
        var startY = 0;

        if (typeof parent.map_obj !== 'undefined' && parent.map_obj !== '') {
            parent.map_obj = $.parseJSON(parent.map_obj);

            if (parent.map_obj.length === 1) {
                icon = 'hotel.png';
            }

            if (typeof parent.map_obj[0] !== 'undefined' && parent.map_obj[0] !== '') {
                startX = parent.map_obj[0].x;
                startY = parent.map_obj[0].y;
            }

        }

        if (typeof parent.map_type === 'undefined') {
            parent.map_type = google.maps.MapTypeId.SATELLITE;
        }

        this.map = "";
        this.bound = new google.maps.LatLngBounds();
        this.infowidnow = new google.maps.InfoWindow({});
        this.markers = new Array();
        this.Data = {
            Page: 'HOME',
            startX: 0,
            startY: 0,
            icon: API.Data.base_url + 'assets/img/google/' + icon
        };

        this.MapOptions = {
            center: new google.maps.LatLng(startX, startY),
            zoom: 17,
            mapTypeId: parent.map_type,
            panControl: true,
            zoomControl: true,
            mapTypeControl: false,
            scaleControl: true,
            streetViewControl: false,
            overviewMapControl: false,
            scrollwheel: false
        };

        this.AddMarker = function(LatLng, title, ID, type) {

            var myIcon = parent.Data.icon;
            if (type != 2)
                myIcon = API.Data.base_url + 'assets/img/google/' + 'bed.png';

            marker = new google.maps.Marker({
                position: LatLng,
                map: parent.map,
                icon: myIcon,
                title: title,
                hotspotid: ID
            });
            parent.bound.extend(LatLng);
            parent.BindInfoWindow(marker); // add an event listener for this marker

            parent.markers.push(marker); // add marker to paralel array width link index

        };

        this.BindInfoWindow = function(marker) {
            google.maps.event.addListener(marker, 'click', function() {
                parent.OpenInfoWidow(marker);
            });
        };

        this.OpenInfoWidow = function(marker) {

            obj = $.grep(parent.map_obj, function(obj) {     // find object in m array and return it
                return obj.ID === marker.hotspotid;
            });
            html = parent.BuildInfoHTML(obj[0]); // grep reutn found object in Array

            parent.infowidnow.setContent(html);
            parent.infowidnow.open(parent.map, marker);
        };

        this.BuildInfoHTML = function(h) {

            var lnk = '<a href="' + h.link + '">';
            var lnk_i = '<a class="image" href="' + h.link + '">';
            var HTML = '<div class="infowidnow">';

            // Make review if there is any
            if (h.reviews > 0) {
                HTML += '<div class="reviews">';
                HTML += '<div class="score">' + h.score + '</div>';
                HTML += '<div class="no_reviews">Score from<br><strong class="count">' + h.reviews + '</strong> review</div>';
                HTML += '</div>';
            }

            HTML += lnk_i + '<img src="' + h.image + '"></a>';
            HTML += '<h3>' + lnk + h.name + '</a></h1>';
            HTML += '<p>' + h.address + '</p>';
            return HTML + '</div>';
        };

        this.FindMarkerIndex = function(hotel_ID) {

            var m_index;

            $.each(parent.map_obj, function(index, e) {

                if (parseInt(e.ID) === parseInt(hotel_ID)) {
                    m_index = index;
                }
            });

            return m_index;
        };

        this.Init = function(openID) {

            parent.markers = new Array();
            parent.bound = new google.maps.LatLngBounds();
            parent.infowidnow = new google.maps.InfoWindow({});

            parent.map = new google.maps.Map(document.getElementById(parent.google_map_component), parent.MapOptions);
            $.each(parent.map_obj, function(index, e) {

                var latLang = new google.maps.LatLng(e.x, e.y);
                parent.AddMarker(latLang, e.name, e.ID, e.type);
            });

            // Clusters
            if (typeof (MarkerClusterer) !== 'undefined')
                var markerCluster = new MarkerClusterer(parent.map, parent.markers);

            // Centritaj i zumiraj mapu ako ima vise obejkata
            if (parent.map_obj.length > 1) {
                parent.map.fitBounds(parent.bound);
            }

            $(parent.gh_obj).on('click', parent.gh_objHandler);
            if (parent.map_obj.length === 1) {
                window.setTimeout(function() {
                    parent.OpenInfoWidow(parent.markers[0]);
                }, 500);
            }

            if (openID !== "ALL") {
                index = parent.FindMarkerIndex(openID);

                window.setTimeout(function() {
                    parent.OpenInfoWidow(parent.markers[index]);
                }, 500);
            }
        };

    }
};
